package com.example.mapreduce.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: LogRecordWriter
 * Package: com.example.mapreduce.outputformat
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-16
 * Time: 20:34
 */

public class LogRecordWriter extends RecordWriter<Text, NullWritable> {
    private FSDataOutputStream atguigu;
    private FSDataOutputStream other;
    public LogRecordWriter(TaskAttemptContext job) {
        //准备创建两条流
        try {
            FileSystem fs = FileSystem.get(job.getConfiguration());
            //第一条流
            atguigu = fs.create(new Path("E:\\input\\output1010\\atguigu.log"));
            other = fs.create(new Path("E:\\input\\output1010\\other.log"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        //这个方法就是真正要往外分别写两个文件的方法
        //具体写 判断是否具有atguigu
        String log = key.toString();
        if (log.contains("atguigu")){
            //每次写一条加回车换行
            atguigu.writeBytes(log + "\n");
        }else {
            other.writeBytes(log + "\n");
        }
    }
    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        //关流
        IOUtils.closeStream(atguigu);
        IOUtils.closeStream(other);
    }
}
